<?php
namespace Home\Model;

//积分
class ScoreModel extends CommonModel{
    
    /**
     * 患者-记录血压：每日记录血压次数>=1次，1日可获得5积分
     * @param $uid 当前用户id
     */
    public function addBlood($uid){
        $User = M('User');
        $current_time = time();
        $create_date = date('Y-m-d',$current_time);
        //如果患者没有完善身份证号则不记录
        $userInfo = $User->find($uid);
        if(empty($userInfo['card_num'])) return;
        
        //在患者身体记录表增加一条数据
        M('Record')->add(array('uid'=>$uid,'did'=>$userInfo['uid'],'w_num'=>date('W',$current_time),'m_num'=>date('m',$current_time)));
        $this->monthClose($userInfo['uid'],$uid);
        
        //如果本天已记录数据则不记录
        $scoreRow = $this->where(array('uid'=>$uid,'type'=>'blood','create_date'=>$create_date))->find();
        if(!empty($scoreRow)) return;
        
        $this->add(array('uid'=>$uid,'type'=>'blood','score'=>5,'create_date'=>$create_date));
        $User->where(array('id'=>$uid))->setInc('score',5);
    }
    
    /**
     * 患者-记录体重：每日记录体重次数>=1次，1日可获得5积分
     * @param $uid 当前用户id
     */
    public function addWeight($uid){
        $User = M('User');
        $current_time = time();
        $create_date = date('Y-m-d',$current_time);
        //如果患者没有完善身份证号则不记录
        $userInfo = $User->find($uid);
        if(empty($userInfo['card_num'])) return;
        
        //在患者身体记录表增加一条数据
        M('Record')->add(array('uid'=>$uid,'did'=>$userInfo['uid'],'w_num'=>date('W',$current_time),'m_num'=>date('m',$current_time)));
        $this->monthClose($userInfo['uid'],$uid);
        
        //如果本天已记录数据则不记录
        $scoreRow = $this->where(array('uid'=>$uid,'type'=>'weight','create_date'=>$create_date))->find();
        if(!empty($scoreRow)) return;
        
        $this->add(array('uid'=>$uid,'type'=>'weight','score'=>5,'create_date'=>$create_date));
        $User->where(array('id'=>$uid))->setInc('score',5);
    }
    
    /**
     * 患者-记录饮水：每日记录饮水次数>=1次，1日可获得5积分
     * @param $uid 当前用户id
     */
    public function addDrink($uid){
        $User = M('User');
        $current_time = time();
        $create_date = date('Y-m-d',$current_time);
        //如果患者没有完善身份证号则不记录
        $userInfo = $User->find($uid);
        if(empty($userInfo['card_num'])) return;
        
        //在患者身体记录表增加一条数据
        M('Record')->add(array('uid'=>$uid,'did'=>$userInfo['uid'],'w_num'=>date('W',$current_time),'m_num'=>date('m',$current_time)));
        $this->monthClose($userInfo['uid'],$uid);
        
        //如果本天已记录数据则不记录
        $scoreRow = $this->where(array('uid'=>$uid,'type'=>'drink','create_date'=>$create_date))->find();
        if(!empty($scoreRow)) return;
        
        $this->add(array('uid'=>$uid,'type'=>'drink','score'=>5,'create_date'=>$create_date));
        $User->where(array('id'=>$uid))->setInc('score',5);
    }
    
    /**
     * 患者-记录用药：完整记录一周用药内容，1周可获得5积分
     * @param $uid 当前用户id
     */
    public function addMedicine($uid){
        $User = M('User');
        //如果患者没有完善身份证号则不记录
        $userInfo = $User->find($uid);
        if(empty($userInfo['card_num'])) return;
        
        //如果最后一次记录跟本天在同一周则不记录
        $scoreRow = $this->where(array('uid'=>$uid,'type'=>'medicine'))->order('id desc')->find();
        if(!empty($scoreRow) && date('W',strtotime($scoreRow['create_date'])) ==  date('W') ){
            return;
        }
        
        $create_date = date('Y-m-d');
        $this->add(array('uid'=>$uid,'type'=>'medicine','score'=>5,'create_date'=>$create_date));
        $User->where(array('id'=>$uid))->setInc('score',5);
    }
    
    /**
     * 患者-记录饮食：每日记录饮食次数>=1次，1日可获得5积分
     * @param $uid 当前用户id
     */
    public function addFood($uid){
        $User = M('User');
        $create_date = date('Y-m-d');
        //如果患者没有完善身份证号则不记录
        $userInfo = $User->find($uid);
        if(empty($userInfo['card_num'])) return;
        
        //如果本天已记录数据则不记录
        $scoreRow = $this->where(array('uid'=>$uid,'type'=>'food','create_date'=>$create_date))->find();
        if(!empty($scoreRow)) return;
        
        $this->add(array('uid'=>$uid,'type'=>'food','score'=>5,'create_date'=>$create_date));
        $User->where(array('id'=>$uid))->setInc('score',5);
    }
    
    /**
     * 设置体重预警值：医生为患者每设定一位，可一次性获得50积分，上不封顶，患者一次性减少50积分
     * @param $uid 当前用户id即医生id
     * @param $pid 患者id
     */
    public function setSafeWeight($uid,$pid){
        $User = M('User');
        $create_date = date('Y-m-d');
        
        //如果患者已被设置体重预警值不记录
        $scoreRow = $this->where(array('uid'=>$pid,'type'=>'sefe_weight'))->find();
        if(!empty($scoreRow)) return;
        
        //医生添加记录，extra为患者id
        $this->add(array('uid'=>$uid,'extra'=>$pid,'type'=>'sefe_weight','score'=>50,'create_date'=>$create_date));
        $User->where(array('id'=>$uid))->setInc('score',50);
        //患者添加记录，extra为医生id
        $this->add(array('uid'=>$pid,'extra'=>$uid,'type'=>'sefe_weight','score'=>'-50','create_date'=>$create_date));
        $User->where(array('id'=>$pid))->setDec('score',50);
    }
    
    /**
     * 患者-通过平台向医生发起提问，每天第一次发起新问题消耗5积分
     * @param $uid 当前用户id
     * @param $receiver 对方手机号
     */
    public function addChat($uid, $receiver = ''){
        $User = M('User');
        $create_date = date('Y-m-d');
        //如果今天向该用户发起过会话
        if($res = $this->where(array('uid'=>$uid,'type'=>'chat','reason'=>$receiver,'create_date'=>$create_date))->find())
            return;
        
        $this->add(array('uid'=>$uid,'type'=>'chat','score'=>'-5','reason'=>$receiver,'create_date'=>$create_date));
        $User->where(array('id'=>$uid))->setDec('score',5);
    }
    
    /**
     * 医生-名下患者每周连续记录身体指标>=3次，则每例奖励 20 积分，周结，80 积分封顶
     * @param $did 医生id
     * @param $uid 患者id
     */
    public function monthClose($did,$uid){
        $User = M('User');
        $Record = M('Record');
        $create_date = date('Y-m-d');
        $count = $Record->where(array('w_num'=>date('W'),'did'=>$did,'uid'=>$uid))->count();
        if($count == 3){
            $arr = $Record->field('*,sum(num) as sum_num')->where(array('w_num'=>date('W'),'did'=>$did))->group('uid')->select();
            $num = 0;//有几例
            foreach($arr as $k=>$v){
                if($v['sum_num'] >= 3) $num += 1;
            }
            if($num < 5){//只有前4例医生才会加积分
                $this->add(array('uid'=>$did,'type'=>'record','score'=>'20','create_date'=>$create_date));
                $User->where(array('id'=>$did))->setInc('score',20);
            }
        }
    }
    
}